//
//  DetailCell.m
//  Travel assistant
//
//  Created by 杨帆 on 16/3/14.
//  Copyright © 2016年 旅游助手. All rights reserved.
//

#import "DetailCell.h"

// fix me: 宏定义一个控件竖间距
static const CGFloat kVerMargin = 5;
static const CGFloat kHorMargin = 10;

@implementation DetailCell


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        [self addViews];
        
//        [self addAllViews];
    }
    
    return self;
}

-(void)addViews{
    
    
    self.photoView = [[UIImageView alloc]initWithFrame:(CGRectMake(kHorMargin, kVerMargin, 0.937*KScreenWidth, 0.33*KScreenHeight))];
    [self.contentView addSubview:self.photoView];
    
    self.textL = [[UILabel alloc]initWithFrame:(CGRectMake(0.031*KScreenWidth,0, 0.94*KScreenWidth, 0.2*KScreenHeight))];
    self.textL.font = [UIFont systemFontOfSize:12];
    
//    self.textL.textColor = [UIColor whiteColor];
    self.textL.numberOfLines = 0;
    [self.contentView addSubview:self.textL];
    
    self.local_timeL = [[UILabel alloc]initWithFrame:(CGRectMake(0.031*KScreenWidth,self.textL.bounds.size.height+self.photoView.bounds.size.height+0.018*KScreenHeight, 0.78*KScreenWidth, 0.044*KScreenHeight))];
    self.local_timeL.font = [UIFont systemFontOfSize:11];
    self.local_timeL.numberOfLines = 0;
    [self.contentView addSubview:self.local_timeL];
    
//    self.local_timeL.textColor = [UIColor whiteColor];
    
}


-(void)addAllViews{
    
    self.photoView = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, KScreenWidth - 10, KScreenHeight*0.7)];
    
    [self.contentView addSubview:self.photoView];
    
    self.textL = [[UILabel alloc]initWithFrame:CGRectMake(5, KScreenHeight*0.7, KScreenWidth - 10, KScreenHeight *0.2)];
    
    self.textL.numberOfLines = 0;
    
    self.textL.font = [UIFont systemFontOfSize:12];
    
    [self.contentView addSubview:self.textL];
    
    self.local_timeL = [[UILabel alloc]initWithFrame:CGRectMake(5, CGRectGetHeight(self.textL.bounds), KScreenWidth - 10, KScreenHeight*0.1-10)];
    
    self.local_timeL.font = [UIFont systemFontOfSize:11];
    self.local_timeL.numberOfLines = 0;
    [self.contentView addSubview:self.local_timeL];
}



//-(void)layoutSubviews{
//    
//    [super layoutSubviews];
//    
//    self.photoView.frame = CGRectMake(kHorMargin, kVerMargin, 0.937*KScreenWidth, 0.33*KScreenHeight);
//    
//    self.textL.frame = CGRectMake(0.031*KScreenWidth,0, 0.94*KScreenWidth, 0.2*KScreenHeight);
//    
//    self.local_timeL.frame = CGRectMake(0.031*KScreenWidth,self.textL.bounds.size.height+self.photoView.bounds.size.height+0.018*KScreenHeight, 0.78*KScreenWidth, 0.044*KScreenHeight);
//    
//    
////    self.photoView.frame = CGRectMake(5, 5, KScreenWidth - 10, KScreenHeight*0.7);
////    
////    self.textL.frame = CGRectMake(5, KScreenHeight*0.7, KScreenWidth - 10, KScreenHeight *0.2);
////    
////    self.local_timeL.frame = CGRectMake(5, CGRectGetHeight(self.textL.bounds), KScreenWidth - 10, KScreenHeight*0.1-10);
//    
//    
//    
//}


-(void)addSource:(DetailDaysModel *)model{
    
    self.model = model;
    
    [self.photoView sd_setImageWithURL:[NSURL URLWithString:model.photo] placeholderImage:nil];
    
    self.textL.text = model.text;
    self.local_timeL.text = model.local_time;
    
}




-(CGFloat)setData:(DetailDaysModel *)model{
    
    
    self.model = model;
    
    CGFloat margin = 0;
    CGFloat cellHeight = margin;
    
    if (isEmptyString(model.photo)) {
        self.photoView.hidden = YES;
    } else {
        self.photoView.hidden = NO;
        [self.photoView sd_setImageWithURL:[NSURL URLWithString:model.photo] placeholderImage:nil];
        self.photoView.contentMode = UIViewContentModeScaleToFill;
        
        cellHeight = self.photoView.bottom;
        
    }
    
    if (isEmptyString(model.text)) {
        self.textL.hidden = YES;
    } else {
        
        cellHeight += kVerMargin;
        
        self.textL.hidden = NO;
        self.textL.text = model.text;
        CGSize size = [model.text sizeWithFontCompatible:self.textL.font constrainedToSize:CGSizeMake(self.textL.width, CGFLOAT_MAX)];
        
//        CGSize size = [self.textL sizeThatFits:CGSizeMake(KScreenWidth, MAXFLOAT)];
        
        self.textL.frame = CGRectMake(kHorMargin, cellHeight, self.textL.width, size.height);
        cellHeight += size.height;
        
    }
    
    if (isEmptyString(model.local_time)) {
        self.local_timeL.hidden = YES;
    } else {
        
        cellHeight += kVerMargin;
        
        self.local_timeL.hidden = NO;
        self.local_timeL.text = model.local_time;
        self.local_timeL.frame = CGRectMake(kHorMargin, cellHeight, self.local_timeL.width, self.local_timeL.height);
        
        cellHeight += self.local_timeL.height;
        
    }
    
    cellHeight += margin;
    
    [self.contentView setHeight:cellHeight];
    [self setHeight:cellHeight];
    
    return cellHeight;

}










- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
